package controllers;

import connectivity.Dbmanager;
import connectivity.QueryManager;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class DeleteCourseController extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        if (request.getParameter("courseid") == null) {
            Dbmanager dbManager = new Dbmanager();
            dbManager.openConnection();
            QueryManager queryManager = new QueryManager(dbManager);

            request.setAttribute("coursesUitSessie", queryManager.getAllCourses());

            dbManager.closeConnection();

            RequestDispatcher dispatcher = request.getRequestDispatcher("/delete_courses.jsp");
            dispatcher.forward(request, response);
        } else {doPost(request, response);
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        Dbmanager dbManager = new Dbmanager();
        dbManager.openConnection();
        QueryManager queryManager = new QueryManager(dbManager);
        
        queryManager.deletecoursebyid(Integer.parseInt(request.getParameter("courseid")));

        request.setAttribute("coursesUitSessie", queryManager.getAllCourses());

        dbManager.closeConnection();
        RequestDispatcher dispatcher = request.getRequestDispatcher("delete_courses.jsp");
        dispatcher.forward(request, response);
    }
}
